<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manager extends MY_Luckydog
{
	public function __construct()
	{
		parent::__construct();
        $login_status = $this->login_status = $this->check_login_status();	
        if (empty($login_status)){
        	redirect('/luckydog/login');
        }
		$this->load->model('luckydog/Manager_model', 'manager');
	}
	
	public function index()
	{
        $bangpai_admin_id = $this->session->userdata("bangpai_admin_id");
        $_SESSION['v_menu'] = 'manager';
		$manager_info = $this->manager->get_manager_info($bangpai_admin_id);
		$this->load->vars('manager_info', $manager_info);
		$this->load->view('luckydog/admin_manager');
	}
	/**
	 * 编辑管理员信息
	 */
	public function edit_manager()
	{
		$id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $username = $this->input->post('username', TRUE);
        $email = $this->input->post('email', TRUE);
        $this->load->helper('email');
		$password = $this->input->post('password', TRUE);
        $chooserole = $this->input->post('chooserole', TRUE);
        if(!empty($username) && !preg_match("/^[a-z0-9_]*$/i",$username))
        {
            echo json_encode(array('code'=>-1,'message'=>'用户名只能是字母数字或下划线格式的字符!','username'=>$username));
            return;
        }
        if(!empty($username) && !preg_match("/^[a-z0-9_]{6,16}$/i",$username))
        {
            echo json_encode(array('code'=>-1,'message'=>'用户名请设置为6-16个字符!','username'=>$username));
            return;
        }
        if(!valid_email($email))
        {
            echo json_encode(array('code'=>-1,'message'=>'邮箱格式不正确!'));
            return;
        }
        if(!empty($password) && !preg_match("/^[a-z0-9_]*$/i",$password))
        {
            echo json_encode(array('code'=>-3,'message'=>'密码只能是字母数字或下划线格式的字符!'));
            return;
        }
        if(!empty($password) && !preg_match("/^[a-z0-9_]{6,16}$/i",$password))
        {
            echo json_encode(array('code'=>-3,'message'=>'密码请设置为6-16个字符!'));
            return;
        }
        $check_username = $this->account->check_username($username);
        if(!empty($check_username))
        {
            echo json_encode(array('code'=>-1,'message'=>'该用户名以被占用！'));
            exit;
        }
        $check_email = $this->account->check_email($email);
        if(!empty($check_email))
        {
            echo json_encode(array('code'=>-1,'message'=>'该邮箱以被占用！'));
        }else{
            if (!empty($password)) {
	           $data['password'] = md6($password);
            }
            $data['username'] = $this->input->post('username',TRUE);
            $data['roleid'] = $this->input->post('chooserole',TRUE);
    		$res = $this->manager->edit_manager($id, $data);
    		echo json_encode(array('code'=>1));
        }
		
	}
}

/* End of file manager.php */
/* Location: ./application/controllers/luckydog/manager.php */
